package com.pocketsweet.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.easemob.chat.EMChatManager;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_edit_nickname)
public class EditNickName extends BaseActivity {

	private HeaderLayout header;
	private MLUser mlUser = new MLUser();
	private String nickname;

	@ViewInject(R.id.edtNickName)
	private static EditText edtNickName;
	private LoadingDailog mLoadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		mlUser = UserService.getCurrentUser();
		initView();
		setHeaderFunction();
		mLoadingDialog = ToolKits.createLoadingDialog(EditNickName.this,
				"登陆中...");
		mLoadingDialog.setText("加载中");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 此方法，如果显示则隐藏，如果隐藏则显示
		// InputMethodManager imm = (InputMethodManager) EditNickName.this
		// .getSystemService(Context.INPUT_METHOD_SERVICE);
		// // 得到InputMethodManager的实例
		// if (imm.isActive()) {
		// // 如果开启
		// imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
		// InputMethodManager.HIDE_NOT_ALWAYS);
		//
		// }
	}

	private void initView() {
		nickname = getIntent().getStringExtra("nickname");
		edtNickName.setEms(10);
		if (!StringUtils.isEmpty(nickname)) {
			edtNickName.setText(nickname);
			edtNickName.setSelection(nickname.length());
		}

	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("保存");
		header.setMiddleText("修改昵称");
		header.setRighttContainerClickListener(new onRightContainerListener() {
			@Override
			public void onClick() {
				final String nickName = edtNickName.getText().toString();
				nickName.trim();
				if (StringUtils.isEmpty(nickName)) {
					ToolKits.toast(EditNickName.this, "昵称不能为空");
					return;
				}
				if (nickName.length() > 16) {
					ToolKits.toast(EditNickName.this, "昵称不能超过16个字");
					return;
				}
				if (!nickname.equals(nickName)) {
					ProfileEdit.needUpdate = true;
				}
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(edtNickName.getWindowToken(), 0);
				mLoadingDialog.show();
				mlUser.setNickname(nickName);
				mlUser.saveInBackground(new SaveCallback() {
					@Override
					public void done(AVException arg0) {
						// TODO Auto-generated method stub
						if (arg0 == null) {
							mLoadingDialog.dismiss();
							// notify
							ProfileEdit.notifyBack();

							EditNickName.this.finish();
							// Intent intent = new Intent(EditNickName.this,
							// UserProfileEdit.class);
							// intent.putStringArrayListExtra("imgsPath",
							// UserProfile.imgsPath);
							// startActivity(intent);
							EMChatManager.getInstance().updateCurrentUserNick(
									nickname);
							ToolKits.toast(EditNickName.this, "修改成功");
						} else {
							mLoadingDialog.dismiss();
							// Log.e("==error", "修改昵称失败" + arg0.getMessage());
						}
					}
				});
			}
		});
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				EditNickName.this.finish();
				// Intent intent = new Intent(EditNickName.this,
				// UserProfileEdit.class);
				// intent.putStringArrayListExtra("imgsPath",
				// UserProfile.imgsPath);
				// startActivity(intent);
			}
		});
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
